package com.mspbots.tsheet.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * com.mspbots.tsheet.config.RabbitConfiguration
 *
 * @author Jason
 * @date 2019/12/23
 **/
@Configuration
public class RabbitConfiguration {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Bean
    public void declareQueue() {

        Exchange exchange = ExchangeBuilder.fanoutExchange("msp.wise.sheet.times").build();
        this.amqpAdmin.declareExchange(exchange);

        Queue queue = QueueBuilder.durable("trigger.tsheet.TsheetInCheck").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("tsheet.TsheetInCheck").noargs());
    }


    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}
